package com.xbongbong.paas.service.field.impl;

import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.field.LinkedAttrPoJo;
import com.xbongbong.paas.service.field.FieldOptimizationService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.paas.util.ExplainUtil;
import com.xbongbong.paas.field.LinkInfoPojo;

import java.util.List;
import java.util.Objects;

/**
 * @author 吴峰
 * @date 2020/07/13 14:31
 */
public class LinkDataOptimizationServiceImpl extends BaseOptimization implements FieldOptimizationService {
    @Override
    public void execute(FieldAttrEntity entity) throws XbbException {
        removeAddRuleAndRemoveRule(entity);
        removeItems(entity);
        removeNumericalLimits(entity);
        removeOfImageAndFile(entity);
        removeDateType(entity);
        removeLineType(entity);
        removeDetailAddress(entity);
        handleLinkData(entity);

        commonHandle(entity);
    }


    private void handleLinkData(FieldAttrEntity fieldAttrEntity) throws XbbException {
        if (Objects.equals(FieldTypeEnum.LINK_DATA.getType(), fieldAttrEntity.getFieldType())) {
            LinkInfoPojo linkInfoPojo = fieldAttrEntity.getLinkInfo();
            setNull(linkInfoPojo.getLinkedAttr());
            setNull(linkInfoPojo.getLinkedShowAttr());
        }
    }

    private void setNull( List<LinkedAttrPoJo> linkedAttrList) {
        if (CollectionsUtil.isEmpty(linkedAttrList)) {
            return;
        }
        for (LinkedAttrPoJo item : linkedAttrList) {
            if (!ExplainUtil.isCombo(item.getFieldType())) {
                item.setItems(null);
                item.setComboType(null);
            }
            if (!Objects.equals(FieldTypeEnum.DATETIME.getType(), item.getFieldType())) {
                item.setDateType(null);
            }
        }
    }
}
